package org.dromara.tearoom.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.bo.TerRoomDetailsBo;
import org.dromara.tearoom.domain.bo.TerRoomDetailsWxBo;
import org.dromara.tearoom.domain.vo.TerRoomDetailsVo;
import org.dromara.tearoom.service.ITerRoomDetailsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 茶室详情
 * 前端访问路由地址为:/tearoom/roomDetails
 *
 * @author xch
 * @date 2025-03-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/roomDetails")
public class TerRoomDetailsController extends BaseController {

    private final ITerRoomDetailsService terRoomDetailsService;

    @GetMapping("/wechat/rmlist")
    public R<List<TerRoomDetailsVo>> wxlist(TerRoomDetailsWxBo bo) {
        bo.setDelFlag("0");
        return R.ok(terRoomDetailsService.queryList(bo));
    }
    /**
     * 查询茶室详情列表
     */
    @SaCheckPermission("tearoom:roomDetails:list")
    @GetMapping("/list")
    public TableDataInfo<TerRoomDetailsVo> list(TerRoomDetailsBo bo, PageQuery pageQuery) {
        return terRoomDetailsService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询茶室详情列表根据区域ID
     */
    @GetMapping("/listByGroupId/{groupIds}")
    public R<List<TerRoomDetailsVo>> listByGroupId( @PathVariable("groupIds") Long[] groupIds) {
        return R.ok(terRoomDetailsService.queryListByGroupIds(List.of(groupIds)));
    }

    @SaCheckPermission("tearoom:roomDetails:list")
    @GetMapping("/queryTotal")
    public R<Integer> queryTotal(){
        return R.ok(terRoomDetailsService.queryTotal());
    }
    /**
     * 导出茶室详情列表
     */
    @SaCheckPermission("tearoom:roomDetails:export")
    @Log(title = "茶室详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerRoomDetailsBo bo, HttpServletResponse response) {
        List<TerRoomDetailsVo> list = terRoomDetailsService.queryList(bo);
        ExcelUtil.exportExcel(list, "茶室详情", TerRoomDetailsVo.class, response);
    }

    /**
     * 获取茶室详情详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:roomDetails:query")
    @GetMapping("/{id}")
    public R<TerRoomDetailsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terRoomDetailsService.queryById(id));
    }

    /**
     * 新增茶室详情
     */
    @SaCheckPermission("tearoom:roomDetails:add")
    @Log(title = "茶室详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerRoomDetailsBo bo) {
        return toAjax(terRoomDetailsService.insertByBo(bo));
    }

    /**
     * 修改茶室详情
     */
    @SaCheckPermission("tearoom:roomDetails:edit")
    @Log(title = "茶室详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerRoomDetailsBo bo) {
        return toAjax(terRoomDetailsService.updateByBo(bo));
    }

    /**
     * 删除茶室
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:roomDetails:remove")
    @Log(title = "逻辑删除茶室", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terRoomDetailsService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 恢复茶室
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:roomDetails:recover")
    @Log(title = "恢复茶室", businessType = BusinessType.DELETE)
    @PutMapping("recover/{ids}")
    public R<Void> recover(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terRoomDetailsService.recoverWithValidByIds(List.of(ids), true));
    }


    /**
     * 永久删除茶室
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:roomDetails:foreverDelete")
    @Log(title = "永久删除茶室", businessType = BusinessType.DELETE)
    @DeleteMapping("foreverDelete/{ids}")
    public R<Void> foreverDelete(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terRoomDetailsService.foreverDeleteWithValidByIds(List.of(ids), true));
    }
}
